#region File Description
//-----------------------------------------------------------------------------
// LoseScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

using System;
using System.IO;
using System.IO.IsolatedStorage;
using GameStateManagement;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace GameStateManagementSample
{
    /// <summary>
    /// A basic pause screen for Windows Phone
    /// </summary>
    class LoseScreen : PhoneMenuScreen
    {
        // for hi score
        //float hiScoreTime;

        public LoseScreen(string time)
            : base("You lasted for " + time + " seconds!")
        {
            // Create the "newGame" and "Exit" buttons for the screen

            Button newGameButton = new Button("New Game");
            newGameButton.Tapped += newGameButton_Tapped;
            MenuButtons.Add(newGameButton);
            
            Button exitButton = new Button("Exit");
            exitButton.Tapped += exitButton_Tapped;
            MenuButtons.Add(exitButton);
        }
        
        /// <summary>
        /// The "newGame" button handler just calls the OnCancel method so that 
        /// pressing the "newGame" button is the same as pressing the hardware back button.
        /// </summary>
        void newGameButton_Tapped(object sender, EventArgs e)
        {
            ScreenManager.Vibrate(50);
            ScreenManager.totalTime = 0;
            LoadingScreen.Load(ScreenManager, true, PlayerIndex.One, new GameplayScreen());
        }

        /// <summary>
        /// The "Exit" button handler uses the LoadingScreen to take the user out to the main menu.
        /// </summary>
        void exitButton_Tapped(object sender, EventArgs e)
        {
            ScreenManager.Vibrate(50);
            ScreenManager.totalTime = 0;
            LoadingScreen.Load(ScreenManager, false, null, new BackgroundScreen(),
                                                           new PhoneMainMenuScreen());
        }

        protected override void OnCancel()
        {
            LoadingScreen.Load(ScreenManager, false, null, new BackgroundScreen(),
                                                           new PhoneMainMenuScreen());
        }

        void saveFile()
        {
            

            
            //string tempStore = "";
            int currHighScore = (int)ScreenManager.totalTime;
            if (currHighScore > ScreenManager.highScore)
            {
                //Obtain the virtual store for application
                IsolatedStorageFile highScoreStorage = IsolatedStorageFile.GetUserStoreForApplication();
                //IsolatedStorageSettings myIsolatedStorageSetting = IsolatedStorageSettings.ApplicationSettings.;

           
                //Create a new folder and call it "ImageFolder"
                highScoreStorage.CreateDirectory("HighScoreFolder");

                //Create a new file and assign a StreamWriter to the store and this new file (myFile.txt)
                //Also take the text contents from the txtWrite control and write it to myFile.txt
                StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("HighScoreFolder\\myHighScore.txt", FileMode.OpenOrCreate, highScoreStorage));
                writeFile.WriteLine(currHighScore);
                writeFile.Close();
            }
            
            
            
        }
        void loadFile()
        {
            IsolatedStorageFile highScoreStorage = IsolatedStorageFile.GetUserStoreForApplication();

            if (!(highScoreStorage.DirectoryExists("HighScoreFolder")))
            {
                int highScore = 0;
                highScoreStorage.CreateDirectory("HighScoreFolder");
                StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("HighScoreFolder\\myHighScore.txt", FileMode.OpenOrCreate, highScoreStorage));
                writeFile.WriteLine(highScore);
                writeFile.Close();
            }

            else
            {

                IsolatedStorageFileStream fileStream = highScoreStorage.OpenFile("HighScoreFolder\\myHighScore.txt", FileMode.Open, FileAccess.Read);
                using (StreamReader reader = new StreamReader(fileStream))
                {
                    string getHighScore = reader.ReadLine();
                    ScreenManager.highScore = Convert.ToInt32(getHighScore);
                }
            }
        }
        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice graphics = ScreenManager.GraphicsDevice;
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            SpriteFont font = ScreenManager.Font;
            loadFile();
            saveFile();
            spriteBatch.Begin();


            spriteBatch.DrawString(font, "HighScore \n"+ScreenManager.highScore, new Vector2(300, 200),Color.White);

            spriteBatch.End();


           


            base.Draw(gameTime);
        }
    }
}
